// pages/new_branch/new_branch.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isdisabled:false, //设置选择时间的picker框是否可用
    longitude: '',
    latitude: '',
    loadgroupmid: '',
    loadgroupid: '',
    loadname: '',
    districtid: '',
    districtmid: '',
    districtname: '',

    auto:false,
    branchname:'',
    longitude:'',
    latitude:'',
    controllerno:'',
    poweron_time: '00:01',
    poweroff_time: '23:59',
    maxelec: 0,
    minelec: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    that.setData({
      loadgroupmid: options.loadgroupmid,
      loadgroupid: options.loadgroupid,
      loadname: options.loadname,
      districtid: options.districtid,
      districtmid: options.districtmid,
      districtname: options.districtname
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  choose_tude: function () {
    let that = this;

    wx.chooseLocation({
      success(res_location) {
        that.setData({
          longitude: res_location.longitude,
          latitude: res_location.latitude,
          //name: res_location.name
        });
      }
    })

  },


  formSubmit: function (e) {
    let that = this;
    //console.log(e.detail.value, "e");
  
    let header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }

    let long_lati = e.detail.value.input_long_lati;
    let long_lati_arr = long_lati.split(",");

    if (e.detail.value.input_branchname == "") {
      wx.showModal({
        title: '提示',
        content: '支路名称不能为空',
        showCancel: false
      });

      return;
    }

    if (e.detail.value.controllerno == "") {
      wx.showModal({
        title: '提示',
        content: '控制器编号不能为空',
        showCancel: false
      });

      return;
    }

    if (long_lati_arr[0] == "" || long_lati_arr[1] == "") {
      wx.showModal({
        title: '提示',
        content: '经度或者纬度不能为空',
        showCancel: false
      });

      return;
    }
  
    that.setData({
      branchname: e.detail.value.input_branchname,   
      controllerno: e.detail.value.input_controllerno,
      poweron_time: e.detail.value.picker_poweron_time,
      poweroff_time: e.detail.value.picker_poweroff_time,
      maxelec: e.detail.value.input_maxelec,
      minelec: e.detail.value.input_minelec,    
      longitude: long_lati_arr[0],
      latitude: long_lati_arr[1], 
      auto: e.detail.value.switch    
    }); 

    wx.request({     
      //url: 'https://119.3.46.145/server/AddNewBranch',     
      url: 'https://119.3.46.145/server/AddNewBranch?userid=' + app.globalData.userid + '&devNo=' + that.data.controllerno + '&name=' + that.data.branchname + '&auto=' + that.data.auto + '&groupID=' + that.data.loadgroupmid + '&ontime=' + e.detail.value.picker_poweron_time + '&offtime=' + e.detail.value.picker_poweroff_time + '&maxCurrent=' + e.detail.value.input_maxelec + '&minCurrent=' + e.detail.value.input_minelec + '&Longitude=' + that.data.longitude + '&Latitude='+that.data.latitude,    
      method: 'Post',    
      // data:{
      //   userid: app.globalData.userid,
      //   devNo: that.data.controllerno,
      //   name: that.data.branchname,
      //   auto:that.data.auto?1:0,
      //   groupID: that.data.loadgroupmid,
      //   ontime: e.detail.value.picker_poweron_time,
      //   offtime: e.detail.value.picker_poweroff_time,
      //   maxCurrent: e.detail.value.input_maxelec,
      //   minCurrent: e.detail.value.input_minelec,   
      
      //   Longitude: that.data.longitude,
      //   Latitude: that.data.latitude        
      // },
      header: header,
      success: res => {
        //data: { error: "0", info: "添加成功" }  {error: "-1", info: "已存在，无法添加"}        
        let result = res.data;

        if (result.error == "0") {
          wx.showModal({
            title: '提示',
            content: result.info,
            success: function () {
              wx.request({
                url: 'https://119.3.46.145/server/loadTree?userid=' + app.globalData.userid + '&id=' + that.data.loadgroupid,  //获取路段包含的单灯及支路列表构造树
                header: header,
                success: function (res) {
                  let data = JSON.parse(res.data.data);

                  //console.log(data, "data");

                  if (data.length == 0) {
                    return;
                  }

                  let tmp_branch_list = [];

                  data.forEach(function(item, index, self){
                    if(item.attributes.devType == 1){
                      tmp_branch_list.push(item);
                    }
                  });                


                  let pages = getCurrentPages(); //获取上一个页面信息栈(a页面)
                  let prevPage = pages[pages.length - 2] //给上一页面的longitude latitude赋值
                  prevPage.setData({
                    branch_list: tmp_branch_list
                  });
                  wx.navigateBack(); //关闭当前页面，返回上一个页面
                },
                fail: function (error) {
                  console.log(error, "dev_manager  onload loadTree");
                }
              });
            }
          });
        }

        if (result.error == "-1") {
          wx.showModal({
            title: '提示',
            content: result.info,
            showCancel: false
          })
        }
      },
      fail: function (error) {
        console.log(error, "addDepartment error");
      }
    })
  },

  formReset: function (e) {
    let that = this;
    // that.setData({
    //   loadgroupName: '',
    //   describe: '',
    //   name: '',
    //   telephone: '',
    //   longitude: '',
    //   latitude: '',
    // });
  },

  set_auto:function(e){
    let that = this;
    //console.log(e,"e set_auto");
    if(e.detail.value === true){
      that.setData({
        isdisabled:true
      });
    }

    if (e.detail.value === false) {
      that.setData({
        isdisabled: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})